package org.wkg.lesson11;

import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

public class Demo_1108 {

    public static void main(String[] args) {
        Semaphore semaphore = new Semaphore(5);

        Runnable runnable= new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName()+"开始申请资源....");
                try {
                    semaphore.acquire();
                    System.out.println(Thread.currentThread().getName()+"=========已获得申请资源=========");
                    TimeUnit.SECONDS.sleep(1);
                    semaphore.release();
                    System.out.println(Thread.currentThread().getName()+"********资源释放********");
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        };

        for (int i = 0; i < 20; i++) {
            Thread thread = new Thread(runnable);
            thread.start();
        }
    }
}
